home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // DriveView.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include <afxcview.h>
- #include "Resource.h"
- #include "DriveView.h"
-
- // Image indexes
- #define ILI_HARD_DISK 0
- #define ILI_FLOPPY 1
- #define ILI_CD_ROM 2
- #define ILI_NET_DRIVE 0
- #define ILI_RAM_DRIVE 0
- #define ILI_CLOSED_FOLDER 3
- #define ILI_OPEN_FOLDER 4
-
- IMPLEMENT_DYNCREATE (CDriveView, CTreeView)
-
- BEGIN_MESSAGE_MAP (CDriveView, CTreeView)
- ON_WM_CREATE ()
- ON_NOTIFY_REFLECT (TVN_ITEMEXPANDING, OnItemExpanding)
- ON_NOTIFY_REFLECT (TVN_SELCHANGED, OnSelChanged)
- END_MESSAGE_MAP ()
-
- BOOL CDriveView::PreCreateWindow (CREATESTRUCT& cs)
- {
- if (!CTreeView::PreCreateWindow (cs))
- return FALSE;
-
- cs.style |= TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS |
- TVS_SHOWSELALWAYS;
- return TRUE;
- }
-
- int CDriveView::OnCreate (LPCREATESTRUCT lpcs)
- {
- if (CTreeView::OnCreate (lpcs) == -1)
- return -1;
-
- m_imglDrives.Create (IDR_DRIVEIMAGES, 16, 1, RGB (255, 0, 255));
- GetTreeCtrl ().SetImageList (&m_imglDrives, TVSIL_NORMAL);
-
- InitTree ();
- return 0;
- }
-
- void CDriveView::OnItemExpanding (NMHDR* pnmh, LRESULT* pResult)
- {
- NM_TREEVIEW* pnmtv = (NM_TREEVIEW*) pnmh;
- HTREEITEM hItem = pnmtv->itemNew.hItem;
- CString string = GetPathFromNode (hItem);
-
- *pResult = FALSE;
-
- if (pnmtv->action == TVE_EXPAND) {
- DeleteFirstChild (hItem);
- if (AddDirectories (hItem, string) == 0)
- *pResult = TRUE;
- }
- else { // pnmtv->action == TVE_COLLAPSE
- DeleteAllChildren (hItem);
- if (GetTreeCtrl ().GetParentItem (hItem) == NULL)
- GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
- ILI_CLOSED_FOLDER, hItem);
- else
- SetButtonState (hItem, string);
- }
- }
-
- void CDriveView::OnSelChanged (NMHDR* pnmh, LRESULT* pResult)
- {
- NM_TREEVIEW* pnmtv = (NM_TREEVIEW*) pnmh;
- CString strPath = GetPathFromNode (pnmtv->itemNew.hItem);
- OnSelectionChanged (strPath);
- }
-
- void CDriveView::OnSelectionChanged (CString& strPath)
- {
- //
- // Override this function in a derived class to respond to
- // selection changes differently. Here, UpdateAllViews is used
- // as a conduit for updating the companion CFileView.
- //
- GetDocument ()->UpdateAllViews (this, (LPARAM) (LPCTSTR) strPath);
- }
-
- int CDriveView::InitTree ()
- {
- int nPos = 0;
- int nDrivesAdded = 0;
- CString strDrive = "?:\\";
-
- DWORD dwDriveList = ::GetLogicalDrives ();
-
- while (dwDriveList) {
- if (dwDriveList & 1) {
- strDrive.SetAt (0, 0x41 + nPos);
- if (AddDriveNode (strDrive))
- nDrivesAdded++;
- }
- dwDriveList >>= 1;
- nPos++;
- }
- return nDrivesAdded;
- }
-
- BOOL CDriveView::AddDriveNode (CString& strDrive)
- {
- CString string;
- HTREEITEM hItem;
- static BOOL bFirst = TRUE;
-
- UINT nType = ::GetDriveType ((LPCTSTR) strDrive);
-
- switch (nType) {
-
- case DRIVE_REMOVABLE:
- hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_FLOPPY,
- ILI_FLOPPY);
- GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
- ILI_CLOSED_FOLDER, hItem);
- break;
-
- case DRIVE_FIXED:
- hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_HARD_DISK,
- ILI_HARD_DISK);
- SetButtonState (hItem, strDrive);
-
- // If this is the first fixed disk, select and expand it
- if (bFirst) {
- GetTreeCtrl ().SelectItem (hItem);
- GetTreeCtrl ().Expand (hItem, TVE_EXPAND);
- bFirst = FALSE;
- }
- break;
-
- case DRIVE_REMOTE:
- hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_NET_DRIVE,
- ILI_NET_DRIVE);
- SetButtonState (hItem, strDrive);
- break;
-
- case DRIVE_CDROM:
- hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_CD_ROM,
- ILI_CD_ROM);
- GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
- ILI_CLOSED_FOLDER, hItem);
- break;
-
- case DRIVE_RAMDISK:
- hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_RAM_DRIVE,
- ILI_RAM_DRIVE);
- SetButtonState (hItem, strDrive);
- break;
-
- default:
- return FALSE;
- }
-
- return TRUE;
- }
-
- BOOL CDriveView::SetButtonState (HTREEITEM hItem, CString& strPath)
- {
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- BOOL bResult = FALSE;
-
- CString string = strPath;
- if (string.Right (1) != "\\")
- string += "\\";
- string += "*.*";
-
- if ((hFind = ::FindFirstFile ((LPCTSTR) string, &fd)) ==
- INVALID_HANDLE_VALUE)
- return bResult;
-
- do {
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- CString strCmp = (LPCTSTR) &fd.cFileName;
- if ((strCmp != ".") && (strCmp != "..")) {
- GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
- ILI_CLOSED_FOLDER, hItem);
- bResult = TRUE;
- break;
- }
- }
- } while (::FindNextFile (hFind, &fd));
-
- ::CloseHandle (hFind);
- return bResult;
- }
-
- CString CDriveView::GetPathFromNode (HTREEITEM hItem)
- {
- CString strResult = GetTreeCtrl ().GetItemText (hItem);
-
- HTREEITEM hParent;
- while ((hParent = GetTreeCtrl ().GetParentItem (hItem)) != NULL) {
- CString string = GetTreeCtrl ().GetItemText (hParent);
- if (string.Right (1) != "\\")
- string += "\\";
- strResult = string + strResult;
- hItem = hParent;
- }
- return strResult;
- }
-
- int CDriveView::AddDirectories (HTREEITEM hItem, CString& strPath)
- {
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- HTREEITEM hNewItem;
-
- int nCount = 0;
-
- CString string = strPath;
- if (string.Right (1) != "\\")
- string += "\\";
- string += "*.*";
-
- if ((hFind = ::FindFirstFile ((LPCTSTR) string, &fd)) ==
- INVALID_HANDLE_VALUE) {
- if (GetTreeCtrl ().GetParentItem (hItem) == NULL)
- GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
- ILI_CLOSED_FOLDER, hItem);
- return 0;
- }
-
- do {
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- CString strCmp = (LPCTSTR) &fd.cFileName;
- if ((strCmp != ".") && (strCmp != "..")) {
- hNewItem =
- GetTreeCtrl ().InsertItem ((LPCTSTR) &fd.cFileName,
- ILI_CLOSED_FOLDER, ILI_OPEN_FOLDER, hItem);
-
- CString strNewPath = strPath;
- if (strNewPath.Right (1) != "\\")
- strNewPath += "\\";
-
- strNewPath += (LPCTSTR) &fd.cFileName;
- SetButtonState (hNewItem, strNewPath);
- nCount++;
- }
- }
- } while (::FindNextFile (hFind, &fd));
-
- ::CloseHandle (hFind);
- return nCount;
- }
-
- void CDriveView::DeleteFirstChild (HTREEITEM hParent)
- {
- HTREEITEM hItem;
- if ((hItem = GetTreeCtrl ().GetChildItem (hParent)) != NULL)
- GetTreeCtrl ().DeleteItem (hItem);
- }
-
- void CDriveView::DeleteAllChildren (HTREEITEM hParent)
- {
- HTREEITEM hItem;
- if ((hItem = GetTreeCtrl ().GetChildItem (hParent)) == NULL)
- return;
-
- do {
- HTREEITEM hNextItem = GetTreeCtrl ().GetNextSiblingItem (hItem);
- GetTreeCtrl ().DeleteItem (hItem);
- hItem = hNextItem;
- } while (hItem != NULL);
- }
-